package com.theshy.test.domain;

import com.theshy.test.dao.Student;

import java.util.ArrayList;

public class OtherStudentDao implements BassStudentDao{

    private static ArrayList<Student> students = new ArrayList<>();

    static {
        Student s1 = new Student(204911,"wbk",20,"2000");
        Student s2 = new Student(204912,"zmh",20,"2000");
        students.add(s1);
        students.add(s2);
    }

    public boolean Addstudent(Student s) {

        students.add(s);

        return true;

    }

    public boolean haveID(int id) {

        for (int i=0;i<students.size();i++) {
            if(students.get(i)!=null&&students.get(i).getId()==id){
                return true;
            }
        }

        return false;
    }

    public Student[] checkStudent() {

        Student[] s = new Student[students.size()];

        for (int i = 0; i < s.length; i++) {
            s[i]=students.get(i);
        }
        return s;
    }

    public void DeleteStudent(int id) {

           int index = getIndex(id);

           students.remove(index);
    }

    public int getIndex(int id){
         int  index = -1;

         for(int i=0;i<students.size();i++){
             if(students.get(i)!=null&&students.get(i).getId()==id){
                 index = i;
                 break;
             }
         }
        return index;
    }

    public boolean ChangeStudent(Student student) {

        int index = getIndex(student.getId());
        if(index!=-1){
            students.set(index,student);
            return true;
        }else{
            return false;
        }

    }
}
